package cn.oowoo.rabbitmq.ttl;

import cn.oowoo.rabbitmq.config.RabbitConfig;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @Author ZhangBin
 * @Date 2021/5/8 15:18
 * @Description: 消息过期测试-生产者
 *
 **/
public class TTLProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitConfig.createConnection();
        // 创建消息通道
        Channel channel = connection.createChannel();


        // 声明队列（默认交换机AMQP default，Direct）
        Map<String,Object> map = new HashMap<>();
        map.put("x-message-ttl",10000);//队列里面的消息最多存在10秒钟
        channel.queueDeclare(RabbitConfig.QUEUE_TTL_NAME, false, false, false, map);


        // 发送消息
        for(int i=0;i<10;i++){
            String msg =  "一条TTL消息...." + i;
            channel.basicPublish("", RabbitConfig.QUEUE_TTL_NAME, null, msg.getBytes());
        }
        //消息发送完毕关闭信道，关闭链接
        channel.close();
        connection.close();
    }

}
